/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.actions.create;

import org.eclipse.gef.ui.actions.WorkbenchPartAction;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;


/**
 * Action used to create a new Diagram.
 * TODO : Make this action more generic :
 * - add a constructor with the changeable parameters.
 * - call a editor method with the diagram type to be created
 * - or provide the creation method as a parameter (factory ?)
 * 
 * @author dumoulin
 */
public class CreateDiagramAction
extends WorkbenchPartAction
{

	/**
	 * 
	 */
	public static String CREATE_DIAGRAM = "CreateDiagram";
	
	/**
	 * 
	 */
	public static String CREATE_DIAGRAM_Label = "New Diagram";
	
	/**
	 * 
	 */
	public static String CREATE_DIAGRAM_Tooltip = "Create a new Diagram";


	/**
	 * 
	 * 
	 * @param part 
	 */
	public CreateDiagramAction(IWorkbenchPart part) {
		super(part);
		setText(CREATE_DIAGRAM_Label);
		setId(CREATE_DIAGRAM);
		setToolTipText(CREATE_DIAGRAM_Tooltip);
		ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages();
		setImageDescriptor( sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));
		setDisabledImageDescriptor(sharedImages
				.getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED));


		setHoverImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_NEW_WIZARD));
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected boolean calculateEnabled() {
		return true;
	}

	/**
	 * 
	 */
	@Override
	public void run() {
		BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {
			public void run() {
				ModelManager modelManager = (ModelManager)getWorkbenchPart().getAdapter(ModelManager.class);
				if(modelManager!=null) {
					Diagram diagram = modelManager.createClassDiagram("new diagram");
				} else {
					throw new RuntimeException("Impossible to adapt the workbench part "+getWorkbenchPart()+" into a ModelManager");
				}
			}
		});
	}

}

